package ltd.hxya.novel.book.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import ltd.hxya.novel.book.entity.Author;
import ltd.hxya.novel.book.mapper.AuthorMapper;
import ltd.hxya.novel.book.service.IAuthorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 作者表 服务实现类
 * </p>
 *
 * @author hxya
 * @since 2022-09-18
 */
@Service
public class AuthorServiceImpl extends ServiceImpl<AuthorMapper, Author> implements IAuthorService {

    @Override
    public void insertAuthor(Author author) {
        //根据name判断是否为空
        Author author1 = getAuthorByName(author.getPenName());
        if (author1!=null){
            author.setId(author1.getId());
            return;
        }
        author.setCreateTime(LocalDateTime.now());
        author.setId(IdWorker.getIdStr());
        this.save(author);
    }

    private Author getAuthorByName(String penName) {

        QueryWrapper<Author> authorQueryWrapper = new QueryWrapper<>();
        authorQueryWrapper.eq("pen_name",penName);
        return this.baseMapper.selectOne(authorQueryWrapper);
    }


}
